<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css" integrity="sha256-2H3fkXt6FEmrReK448mDVGKb3WW2ZZw35gI7vqHOE4Y=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{&quot;hostname&quot;:&quot;www.nilstorm.com&quot;,&quot;root&quot;:&quot;&#x2F;&quot;,&quot;images&quot;:&quot;&#x2F;images&quot;,&quot;scheme&quot;:&quot;Gemini&quot;,&quot;version&quot;:&quot;8.5.0&quot;,&quot;exturl&quot;:false,&quot;sidebar&quot;:{&quot;position&quot;:&quot;left&quot;,&quot;display&quot;:&quot;post&quot;,&quot;padding&quot;:18,&quot;offset&quot;:12},&quot;copycode&quot;:false,&quot;bookmark&quot;:{&quot;enable&quot;:false,&quot;color&quot;:&quot;#222&quot;,&quot;save&quot;:&quot;auto&quot;},&quot;fancybox&quot;:false,&quot;mediumzoom&quot;:false,&quot;lazyload&quot;:false,&quot;pangu&quot;:false,&quot;comments&quot;:{&quot;style&quot;:&quot;tabs&quot;,&quot;active&quot;:null,&quot;storage&quot;:true,&quot;lazyload&quot;:false,&quot;nav&quot;:null},&quot;motion&quot;:{&quot;enable&quot;:true,&quot;async&quot;:false,&quot;transition&quot;:{&quot;post_block&quot;:&quot;fadeIn&quot;,&quot;post_header&quot;:&quot;fadeInDown&quot;,&quot;post_body&quot;:&quot;fadeInDown&quot;,&quot;coll_header&quot;:&quot;fadeInLeft&quot;,&quot;sidebar&quot;:&quot;fadeInUp&quot;}},&quot;prism&quot;:false,&quot;i18n&quot;:{&quot;placeholder&quot;:&quot;Searching...&quot;,&quot;empty&quot;:&quot;We didn&#39;t find any results for the search: ${query}&quot;,&quot;hits_time&quot;:&quot;${hits} results found in ${time} ms&quot;,&quot;hits&quot;:&quot;${hits} results found&quot;}}</script><script src="/js/config.js"></script>
<meta name="description" content="BX_BOXING_IMMEDIATELY_UNBOXEDPrimitive value is boxed and then immediately unboxed对原始值进行装箱，然后立即取消装箱。这可能是在一个未要求装箱的地方进行了手动装箱，从而迫使编译器进行立即撤消装箱的操作">
<meta property="og:type" content="article">
<meta property="og:title" content="FindBugs 规则整理：Performance">
<meta property="og:url" content="http://www.nilstorm.com/2017/8a8d645f.html">
<meta property="og:site_name" content="Lam&#39;s Blog">
<meta property="og:description" content="BX_BOXING_IMMEDIATELY_UNBOXEDPrimitive value is boxed and then immediately unboxed对原始值进行装箱，然后立即取消装箱。这可能是在一个未要求装箱的地方进行了手动装箱，从而迫使编译器进行立即撤消装箱的操作">
<meta property="og:locale">
<meta property="og:image" content="https://tva1.sinaimg.cn/large/007S8ZIlgy1gee6wkfo69j30e9026gm4.jpg">
<meta property="article:published_time" content="2017-03-03T02:37:18.000Z">
<meta property="article:modified_time" content="2021-06-06T08:08:18.170Z">
<meta property="article:author" content="LinBinghe">
<meta property="article:tag" content="FindBugs">
<meta property="article:tag" content="规则">
<meta property="article:tag" content="Performance">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://tva1.sinaimg.cn/large/007S8ZIlgy1gee6wkfo69j30e9026gm4.jpg">


<link rel="canonical" href="http://www.nilstorm.com/2017/8a8d645f.html">



<script class="next-config" data-name="page" type="application/json">{&quot;sidebar&quot;:&quot;&quot;,&quot;isHome&quot;:false,&quot;isPost&quot;:true,&quot;lang&quot;:&quot;zh-Hans&quot;,&quot;comments&quot;:true,&quot;permalink&quot;:&quot;http:&#x2F;&#x2F;www.nilstorm.com&#x2F;2017&#x2F;8a8d645f.html&quot;,&quot;path&quot;:&quot;2017&#x2F;8a8d645f.html&quot;,&quot;title&quot;:&quot;FindBugs 规则整理：Performance&quot;}</script>

<script class="next-config" data-name="calendar" type="application/json">&quot;&quot;</script>
<title>FindBugs 规则整理：Performance | Lam's Blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="Lam's Blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Lam's Blog</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">Knowledge as Action</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#BX-BOXING-IMMEDIATELY-UNBOXED"><span class="nav-number">1.</span> <span class="nav-text">BX_BOXING_IMMEDIATELY_UNBOXED</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#BX-BOXING-IMMEDIATELY-UNBOXED-TO-PERFORM-COERCION"><span class="nav-number">2.</span> <span class="nav-text">BX_BOXING_IMMEDIATELY_UNBOXED_TO_PERFORM_COERCION</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#DM-BOXED-PRIMITIVE-TOSTRING"><span class="nav-number">3.</span> <span class="nav-text">DM_BOXED_PRIMITIVE_TOSTRING</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#DM-FP-NUMBER-CTOR"><span class="nav-number">4.</span> <span class="nav-text">DM_FP_NUMBER_CTOR</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#DM-NUMBER-CTOR"><span class="nav-number">5.</span> <span class="nav-text">DM_NUMBER_CTOR</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#DMI-BLOCKING-METHODS-ON-URL"><span class="nav-number">6.</span> <span class="nav-text">DMI_BLOCKING_METHODS_ON_URL</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#DMI-COLLECTION-OF-URLS"><span class="nav-number">7.</span> <span class="nav-text">DMI_COLLECTION_OF_URLS</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#DM-BOOLEAN-CTOR"><span class="nav-number">8.</span> <span class="nav-text">DM_BOOLEAN_CTOR</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#DM-GC"><span class="nav-number">9.</span> <span class="nav-text">DM_GC</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#DM-NEXTINT-VIA-NEXTDOUBLE"><span class="nav-number">10.</span> <span class="nav-text">DM_NEXTINT_VIA_NEXTDOUBLE</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#DM-STRING-CTOR"><span class="nav-number">11.</span> <span class="nav-text">DM_STRING_CTOR</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#DM-STRING-TOSTRING"><span class="nav-number">12.</span> <span class="nav-text">DM_STRING_TOSTRING</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#DM-STRING-VOID-CTOR"><span class="nav-number">13.</span> <span class="nav-text">DM_STRING_VOID_CTOR</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#ITA-INEFFICIENT-TO-ARRAY"><span class="nav-number">14.</span> <span class="nav-text">ITA_INEFFICIENT_TO_ARRAY</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#SBSC-USE-STRINGBUFFER-CONCATENATION"><span class="nav-number">15.</span> <span class="nav-text">SBSC_USE_STRINGBUFFER_CONCATENATION</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#SS-SHOULD-BE-STATIC"><span class="nav-number">16.</span> <span class="nav-text">SS_SHOULD_BE_STATIC</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#UM-UNNECESSARY-MATH"><span class="nav-number">17.</span> <span class="nav-text">UM_UNNECESSARY_MATH</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#UPM-UNCALLED-PRIVATE-METHOD"><span class="nav-number">18.</span> <span class="nav-text">UPM_UNCALLED_PRIVATE_METHOD</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#URF-UNREAD-FIELD"><span class="nav-number">19.</span> <span class="nav-text">URF_UNREAD_FIELD</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#UUF-UNUSED-FIELD"><span class="nav-number">20.</span> <span class="nav-text">UUF_UNUSED_FIELD</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#WMI-WRONG-MAP-ITERATOR"><span class="nav-number">21.</span> <span class="nav-text">WMI_WRONG_MAP_ITERATOR</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#DM-BOXED-PRIMITIVE-FOR-PARSING"><span class="nav-number">22.</span> <span class="nav-text">DM_BOXED_PRIMITIVE_FOR_PARSING</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%B6%E4%BB%96%E6%96%87%E7%AB%A0%EF%BC%88%E6%8C%81%E7%BB%AD%E6%9B%B4%E6%96%B0%EF%BC%89"><span class="nav-number">23.</span> <span class="nav-text">其他文章（持续更新）</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%BC%95%E7%94%A8"><span class="nav-number">24.</span> <span class="nav-text">引用</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="LinBinghe"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">LinBinghe</p>
  <div class="site-description" itemprop="description">Knowledge as Action</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives">
          <span class="site-state-item-count">39</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
        <span class="site-state-item-count">13</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
        <span class="site-state-item-count">60</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/LinBinghe" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;LinBinghe" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.zhihu.com/people/shawn_lam" title="Zhihu → https:&#x2F;&#x2F;www.zhihu.com&#x2F;people&#x2F;shawn_lam" rel="noopener" target="_blank"><i class="fab fa-zhihu fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:linbinghe@gmail.com" title="E-Mail → mailto:linbinghe@gmail.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i></a>
      </span>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="http://www.nilstorm.com/2017/8a8d645f.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="LinBinghe">
      <meta itemprop="description" content="Knowledge as Action">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lam's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          FindBugs 规则整理：Performance
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2017-03-03 10:37:18" itemprop="dateCreated datePublished" datetime="2017-03-03T10:37:18+08:00">2017-03-03</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-06-06 16:08:18" itemprop="dateModified" datetime="2021-06-06T16:08:18+08:00">2021-06-06</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">In</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/FindBugs/" itemprop="url" rel="index"><span itemprop="name">FindBugs</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="BX-BOXING-IMMEDIATELY-UNBOXED"><a href="#BX-BOXING-IMMEDIATELY-UNBOXED" class="headerlink" title="BX_BOXING_IMMEDIATELY_UNBOXED"></a>BX_BOXING_IMMEDIATELY_UNBOXED</h1><p><em>Primitive value is boxed and then immediately unboxed</em><br>对原始值进行装箱，然后立即取消装箱。这可能是在一个未要求装箱的地方进行了手动装箱，从而迫使编译器进行立即撤消装箱的操作<br><span id="more"></span></p>
<h1 id="BX-BOXING-IMMEDIATELY-UNBOXED-TO-PERFORM-COERCION"><a href="#BX-BOXING-IMMEDIATELY-UNBOXED-TO-PERFORM-COERCION" class="headerlink" title="BX_BOXING_IMMEDIATELY_UNBOXED_TO_PERFORM_COERCION"></a>BX_BOXING_IMMEDIATELY_UNBOXED_TO_PERFORM_COERCION</h1><p><em>Primitive value is boxed then unboxed to perform primitive coercion</em><br>对原始值进行装箱然后立即把它强制转换为另外一种原始类型。例如：<br><code>new Double(d).intValue()</code>应该直接进行强制转换例如：<code>(int) d</code></p>
<h1 id="DM-BOXED-PRIMITIVE-TOSTRING"><a href="#DM-BOXED-PRIMITIVE-TOSTRING" class="headerlink" title="DM_BOXED_PRIMITIVE_TOSTRING"></a>DM_BOXED_PRIMITIVE_TOSTRING</h1><p><em>Method allocates a boxed primitive just to call toString</em><br>仅仅为了调用封装类的toString()而对原始类型进行封装操作。比这种方法更有效的是调用封装类的toString(…)方法例如：<br><code>new Integer(1).toString()</code>    替换为   <code>Integer.toString(1)</code><br><code>new Long(1).toString()</code>   替换为   <code>Long.toString(1)</code><br><code>new Float(1.0).toString()</code>    替换为   <code>Float.toString(1.0)</code><br><code>new Double(1.0).toString()</code>    替换为   <code>Double.toString(1.0)</code><br><code>new Byte(1).toString()</code>   替换为   <code>Byte.toString(1)</code><br><code>new Short(1).toString()</code>   替换为   <code>Short.toString(1)</code><br><code>new Boolean(true).toString()</code>   替换为  <code>Boolean.toString(true)</code></p>
<h1 id="DM-FP-NUMBER-CTOR"><a href="#DM-FP-NUMBER-CTOR" class="headerlink" title="DM_FP_NUMBER_CTOR"></a>DM_FP_NUMBER_CTOR</h1><p><em>Method invokes inefficient floating-point Number constructor; use static valueOf instead</em><br>使用new Double(double)方法总是会创建一个新的对象，然而使用Double.valueOf(double)方法可以把值保存在编辑器或者class library、JVM中。使用存储值的方式来避免对象的分配可以或得更好的代码性能<br>除非类必须符合Java 1.5以前的JVM，否则请使用自动装箱或valueOf（）方法创建Double和Float实例。</p>
<h1 id="DM-NUMBER-CTOR"><a href="#DM-NUMBER-CTOR" class="headerlink" title="DM_NUMBER_CTOR"></a>DM_NUMBER_CTOR</h1><p><em>Method invokes inefficient Number constructor; use static valueOf instead</em><br>使用new Integer(int)方法总是会创建一个新的对象，然而使用Integer.valueOf(int)方法可以把值保存在编辑器或者class library、JVM中。使用存储值的方式来避免对象的分配可以或得更好的代码性能<br>除非类必须符合Java 1.5以前的JVM，否则请使用自动装箱或valueOf（）方法创建Long, Integer, Short, Character, Byte实例。</p>
<h1 id="DMI-BLOCKING-METHODS-ON-URL"><a href="#DMI-BLOCKING-METHODS-ON-URL" class="headerlink" title="DMI_BLOCKING_METHODS_ON_URL"></a>DMI_BLOCKING_METHODS_ON_URL</h1><p><em>The equals and hashCode methods of URL are blocking</em><br>使用equals和hashCode方法来对url进行资源标识符解析时会引起堵塞。考虑使用java.net.URI来代替。</p>
<h1 id="DMI-COLLECTION-OF-URLS"><a href="#DMI-COLLECTION-OF-URLS" class="headerlink" title="DMI_COLLECTION_OF_URLS"></a>DMI_COLLECTION_OF_URLS</h1><p><em>Maps and sets of URLs can be performance hogs</em><br>方法或者字段使用url的map/set集合。因为equals方法或者hashCode方法来进行资源标识符解析时都会引起堵塞。考虑使用java.net.URI来代替。</p>
<h1 id="DM-BOOLEAN-CTOR"><a href="#DM-BOOLEAN-CTOR" class="headerlink" title="DM_BOOLEAN_CTOR"></a>DM_BOOLEAN_CTOR</h1><p><em>Method invokes inefficient Boolean constructor; use Boolean.valueOf(…) instead</em><br>使用new方法创建一个java.lang.Boolean类型能够的实例对象是浪费空间的，因为Boolean对象是不可变的而且只有两个有用的值。使用Boolean.valueOf()或者Java1.5中的自动装箱功能来创建一个Boolean实例。</p>
<h1 id="DM-GC"><a href="#DM-GC" class="headerlink" title="DM_GC"></a>DM_GC</h1><p><em>Explicit garbage collection; extremely dubious except in benchmarking code</em><br>在代码中显式的调用垃圾回收命名，这样做并不能起作用。在过去，有人在关闭操作或者finalize方法中调用垃圾回收方法导致了很多的性能浪费。这样大规模回收对象时会造成处理器运行缓慢。</p>
<h1 id="DM-NEXTINT-VIA-NEXTDOUBLE"><a href="#DM-NEXTINT-VIA-NEXTDOUBLE" class="headerlink" title="DM_NEXTINT_VIA_NEXTDOUBLE"></a>DM_NEXTINT_VIA_NEXTDOUBLE</h1><p><em>Use the nextInt method of Random rather than nextDouble to generate a random integer</em><br>如果r是一个java.util.Random对象，你可以使r.nextInt(n)生成一个0到n-1之前的随机数，而不是使用(int)(r.nextDouble() * n)</p>
<h1 id="DM-STRING-CTOR"><a href="#DM-STRING-CTOR" class="headerlink" title="DM_STRING_CTOR"></a>DM_STRING_CTOR</h1><p><em>Method invokes inefficient new String(String) constructor</em><br>使用java.lang.String(String)构造函数会浪费内存因为这种构造方式和String作为参数在功能上容易混乱。只是使用String直接作为参数的形式</p>
<h1 id="DM-STRING-TOSTRING"><a href="#DM-STRING-TOSTRING" class="headerlink" title="DM_STRING_TOSTRING"></a>DM_STRING_TOSTRING</h1><p><em>Method invokes toString() method on a String</em><br>调用String.toString()是多余的操作，只要使用String就可以了。</p>
<h1 id="DM-STRING-VOID-CTOR"><a href="#DM-STRING-VOID-CTOR" class="headerlink" title="DM_STRING_VOID_CTOR"></a>DM_STRING_VOID_CTOR</h1><p><em>Method invokes inefficient new String() constructor</em><br>使用没有参数的构造方法去创建新的String对象是浪费内存空间的，因为这样创建会和空字符串“”混淆。Java中保证完成相同的构造方法会产生描绘相同的String对象。所以你只要使用空字符串来创建就可以了。</p>
<h1 id="ITA-INEFFICIENT-TO-ARRAY"><a href="#ITA-INEFFICIENT-TO-ARRAY" class="headerlink" title="ITA_INEFFICIENT_TO_ARRAY"></a>ITA_INEFFICIENT_TO_ARRAY</h1><p><em>Method uses toArray() with zero-length array argument</em><br>当使用集合的toArray()方法时使用数组长度为0的数组作为参数。比这更有效的一种方法是<br>myCollection.toArray(new Foo[myCollection.size()])，如果数组的长度足够大就可以直接把集合中的内容包装到数组中直接返回从而避免了第二次创建一个新的数组来存放集合中值。</p>
<h1 id="SBSC-USE-STRINGBUFFER-CONCATENATION"><a href="#SBSC-USE-STRINGBUFFER-CONCATENATION" class="headerlink" title="SBSC_USE_STRINGBUFFER_CONCATENATION"></a>SBSC_USE_STRINGBUFFER_CONCATENATION</h1><p><em>Method concatenates strings using + in a loop</em><br>在循环中构建一个String对象时从性能上讲使用StringBuffer来代替String对象<br>例如：<br><code>// This is bad
  String s = &quot;&quot;;
  for (int i = 0; i &lt; field.length; ++i) &#123;
    s = s + field[i];
  &#125;</code><br> <code>// This is better
  StringBuffer buf = new StringBuffer();
  for (int i = 0; i &lt; field.length; ++i) &#123;
    buf.append(field[i]);
  &#125;
  String s = buf.toString();</code></p>
<h1 id="SS-SHOULD-BE-STATIC"><a href="#SS-SHOULD-BE-STATIC" class="headerlink" title="SS_SHOULD_BE_STATIC"></a>SS_SHOULD_BE_STATIC</h1><p><em>Unread field: should this field be static?</em><br>类中所包含的final属性字段在编译器中初始化为静态的值。考虑在定义时就把它定义为static类型的。</p>
<h1 id="UM-UNNECESSARY-MATH"><a href="#UM-UNNECESSARY-MATH" class="headerlink" title="UM_UNNECESSARY_MATH"></a>UM_UNNECESSARY_MATH</h1><p><em>Method calls static Math class method on a constant value</em><br>在方法中使用了java.lang.Math的静态方法代替常量来使用，使用常量速度和准确度会更好。 以下为Math中的方法产生的值。<br><code>Method Parameter 
abs -any- 
acos 0.0 or 1.0 
asin 0.0 or 1.0 
atan 0.0 or 1.0 
atan2 0.0 cbrt 0.0 or 1.0 
ceil -any- 
cos 0.0 
cosh 0.0 
exp 0.0 or 1.0 
expm1 0.0 
floor -any- 
log 0.0 or 1.0 
log10 0.0 or 1.0 
rint -any- 
round -any- 
sin 0.0 
sinh 0.0 
sqrt 0.0 or 1.0 
tan 0.0 
tanh 0.0 
toDegrees 0.0 or 1.0 
toRadians 0.0</code></p>
<h1 id="UPM-UNCALLED-PRIVATE-METHOD"><a href="#UPM-UNCALLED-PRIVATE-METHOD" class="headerlink" title="UPM_UNCALLED_PRIVATE_METHOD"></a>UPM_UNCALLED_PRIVATE_METHOD</h1><p><em>Private method is never called</em><br>定义为Private类型方法从未被调用，应该被删除。</p>
<h1 id="URF-UNREAD-FIELD"><a href="#URF-UNREAD-FIELD" class="headerlink" title="URF_UNREAD_FIELD"></a>URF_UNREAD_FIELD</h1><p><em>Unread field</em><br>类中定义的属性从未被调用，建议删除。</p>
<h1 id="UUF-UNUSED-FIELD"><a href="#UUF-UNUSED-FIELD" class="headerlink" title="UUF_UNUSED_FIELD"></a>UUF_UNUSED_FIELD</h1><p><em>Unused field</em><br>类中定义的属性从未被使用，建议删除。</p>
<h1 id="WMI-WRONG-MAP-ITERATOR"><a href="#WMI-WRONG-MAP-ITERATOR" class="headerlink" title="WMI_WRONG_MAP_ITERATOR"></a>WMI_WRONG_MAP_ITERATOR</h1><p><em>Inefficient use of keySet iterator instead of entrySet iterator</em><br>当方法中接受一个Map类型的参数时，使用keySet的迭代器比使用entrySet的迭代器效率要高。</p>
<h1 id="DM-BOXED-PRIMITIVE-FOR-PARSING"><a href="#DM-BOXED-PRIMITIVE-FOR-PARSING" class="headerlink" title="DM_BOXED_PRIMITIVE_FOR_PARSING"></a>DM_BOXED_PRIMITIVE_FOR_PARSING</h1><p><em>A boxed primitive is created from a String, just to extract the unboxed primitive value. It is more efficient to just call the static parseXXX method.</em><br>不需要使用封装/反封装来解析一个基本类型，使用parseXXX效率更高</p>
<h1 id="其他文章（持续更新）"><a href="#其他文章（持续更新）" class="headerlink" title="其他文章（持续更新）"></a>其他文章（持续更新）</h1><p><a target="_blank" rel="noopener" href="http://linbinghe.me/2017/b8a60a5e.html">FindBugs：简介与使用</a><br><a target="_blank" rel="noopener" href="http://linbinghe.me/2017/f6c077f9.html">FindBugs 规则整理：Bad Practice</a><br><a target="_blank" rel="noopener" href="http://linbinghe.me/2017/eb74f6c5.html">FindBugs 规则整理：Style &amp; Dodgy</a><br><a target="_blank" rel="noopener" href="http://linbinghe.me/2017/7a2a5923.html">FindBugs 规则整理：Internationalization</a><br><a target="_blank" rel="noopener" href="http://linbinghe.me/2017/7dfffed7.html">FindBugs 规则整理：Malicious Code Vulnerability</a><br><a target="_blank" rel="noopener" href="http://linbinghe.me/2017/27613e18.html">FindBugs 规则整理：Security &amp; Experimental</a><br><a target="_blank" rel="noopener" href="http://linbinghe.me/2017/baebe2db.html">FindBugs 规则整理：Multithreaded Correctness</a><br><a target="_blank" rel="noopener" href="http://linbinghe.me/2017/b2ff5786.html">FindBugs 规则整理：CORRECTNESS</a></p>
<h1 id="引用"><a href="#引用" class="headerlink" title="引用"></a>引用</h1><p><em>整合以下文章过程中发现部分存在翻译错误，已做修正，同时感谢以下文章作者</em><br><a target="_blank" rel="noopener" href="http://blog.csdn.net/jdsjlzx/article/details/21472253/">FindBugs规则整理</a></p>
<hr>
<p><strong>版权声明</strong></p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gee6wkfo69j30e9026gm4.jpg" alt="Creative Commons BY-NC-ND 4.0 International License"></p>
<p><a target="_blank" rel="noopener" href="http://linbinghe.me/">Lam’s Blog</a> by <a target="_blank" rel="noopener" href="http://linbinghe.me/">Binghe Lin</a> is licensed under a <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons BY-NC-ND 4.0 International License</a>.<br>由<a target="_blank" rel="noopener" href="http://linbinghe.me/">林炳河</a>创作并维护的<a target="_blank" rel="noopener" href="http://linbinghe.me/">Lam’s Blog</a>采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">创作共用保留署名-非商业-禁止演绎4.0国际许可证</a>。</p>
<p>本文首发于<a target="_blank" rel="noopener" href="http://linbinghe.me/">Lam’s Blog - Knowledeg as Action</a>，版权所有，侵权必究。</p>
<p>本文永久链接：<a target="_blank" rel="noopener" href="http://codinglife.me/2017/8a8d645f.html">http://codinglife.me/2017/8a8d645f.html</a></p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/FindBugs/" rel="tag"># FindBugs</a>
              <a href="/tags/%E8%A7%84%E5%88%99/" rel="tag"># 规则</a>
              <a href="/tags/Performance/" rel="tag"># Performance</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2017/2.7613e+22.html" rel="prev" title="FindBugs 规则整理：Security & Experimental">
                  <i class="fa fa-chevron-left"></i> FindBugs 规则整理：Security & Experimental
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2017/7a2a5923.html" rel="next" title="FindBugs 规则整理：Internationalization">
                  FindBugs 规则整理：Internationalization <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">LinBinghe</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a>
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  






  





</body>
</html>
